#include<iostream>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include <fcntl.h>
#include<thread>
#include<mutex>

using namespace std;

int num = 0;
mutex mut;

void doing(){
    
    for (size_t i = 0; i < 100; i++)
    {
        mut.lock();
        // lock_guard<mutex> lock(mut);
        ++num;
        // this_thread::sleep_for(chrono::microseconds(500));
        this_thread::yield();
        cout<<"线程id: "<<this_thread::get_id()<<" num: "<<i<<endl;
        mut.unlock();
        // mut.unlock();
    }

    cout<<"线程id: "<<this_thread::get_id()<<" 结束 NUM = "<<num <<endl;
    
}

int main(int argc,char **argv){

    cout<<"==============main begin============"<<endl;
    fflush(NULL);
   thread t1(doing);
   thread t2(doing);
   thread t3(doing);
   t1.join();
   t2.join();
   t3.join();
   cout<<"主线程结束"<<endl;



   return 0;
}